package com.framework.base.util;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/**
 * @Title: ExcelOutUtil.java
 * @dataTable:
 * @Description: TODO(用一句话描述该文件做什么)
 * @author www.51wuxia.com
 * @version V1.0
 */

public class ExcelOutUtil {
    /**
     * 导出数据到Excel
     * @param str1 名称列
     * @param str2 单元格宽度
     * @param list 值列
     * @param title 标题
     * @param fileName 文件名
     * @throws IOException 
     * 
     */
	public  static synchronized  void putDataExcel(String[] str1,String[] str2,List<String[]> list,String title,String fileName) throws IOException{
		Struts2Utils.getResponse().reset();
		Struts2Utils.getResponse().reset();
		Struts2Utils.getResponse().setContentType("application/vnd.ms-excel");
		Struts2Utils.getResponse().setHeader("Content-disposition", "attachment; filename="+fileName+".xls; charset=GBK");
		OutputStream outputStream=Struts2Utils.getResponse().getOutputStream();
		try{	
			WritableWorkbook wb = Workbook.createWorkbook(outputStream);
			WritableSheet sheet = wb.createSheet(title, 0);
			//sheet.setColumnView(0, 15);//列宽
			//sheet.setColumnView(1, 70);//列宽
			//sheet.setColumnView(2, 30);//列宽
			for(int i=0;i<str2.length;i++){
				sheet.setColumnView(i, Integer.parseInt(str2[i]));
			}
			sheet.setRowView(0, 25);//行高
			WritableFont headFont = new WritableFont(WritableFont.ARIAL, 18);
			WritableCellFormat wcf_head = new WritableCellFormat(headFont);
			wcf_head.setBorder(Border.ALL, BorderLineStyle.THIN);
			wcf_head.setAlignment(Alignment.CENTRE);
			WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 12);
			WritableCellFormat wcf_title = new WritableCellFormat(titleFont);
			wcf_title.setBorder(Border.ALL, BorderLineStyle.THIN);
			wcf_title.setAlignment(Alignment.CENTRE);
			
			WritableCellFormat wcf_title2 = new WritableCellFormat(titleFont);
			wcf_title2.setBorder(Border.ALL, BorderLineStyle.THIN);
			wcf_title2.setAlignment(Alignment.LEFT);
			sheet.mergeCells(0, 0, str1.length-1, 0);//头文件
			
			sheet.addCell(new Label(0, 0,  title, wcf_head));
            for(int i=0;i<str1.length;i++){//表头
            	sheet.addCell(new Label(i, 1, str1[i], wcf_title));
            }
            //值
			int i = 0;
			for (i = 2; list != null && i - 2 < list.size(); i++){  
				String[] str=list.get(i - 2);
				for(int j=0;j<str.length;j++){
					sheet.addCell(new Label(j, i, str[j], wcf_title));
				}
				
			}
			wb.write();
			wb.close();
		}
		catch (Exception e){
			//throw new Exception(e);
			System.out.println("导出数据报错信息：=========="+e);
		}
		
	}
}
